package com.vm.osm.business.controller;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.eam.equ_part.EamEquPart;
import com.vm.osm.business.dto.req.other.ReqSearchEqu;
import com.vm.osm.business.dto.req.other.ReqSearchEquPart;
import com.vm.osm.business.dto.rsp.HttpRspResult;
import com.vm.osm.business.dto.rsp.other.EquInfoPage;
import com.vm.osm.business.dto.rsp.other.RspEquInfoPage;
import com.vm.osm.business.util.ApiCallUtil;
import com.vm.osm.business.util.GsonUtil;
import com.vm.osm.business.util.MapUtil;
import com.vm.osm.business.util.UserDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenzui on 2018/3/15.
 */
@RestController
@RequestMapping("/v1.0")
@Api(value = "EquInfoController",description = "设备模块",tags = {"EquInfoController"})
public class EquInfoController extends ControllerExceptionHandler {

    private Logger LOG = Logger.getLogger(this.getClass());
    @Value("${eam_api_url}")
    private String eamApiUrl;
    @Autowired
    private RestTemplate restTemplate;
    @ApiOperation(value = "查询设备接口",notes = "查询设备接口")
    @ApiImplicitParam(name = "reqSearchEqu",value = "查询设备接口参数",required = true,dataType = "ReqSearchEqu")
    @RequestMapping(value = "/query_equ_info",method = RequestMethod.POST)
    public Object queryEquInfo(HttpServletRequest request, @RequestBody ReqSearchEqu reqSearchEqu){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        LOG.info("enterpriseId"+enterpriseId);
        // TODO: 2018/2/22  根据企业编号调用设备类型接口
        String postUrl = eamApiUrl+"/eam/v1.0/"+enterpriseId+"/equipments/search";

        Object entity = restTemplate.postForObject(postUrl,reqSearchEqu,Object.class);
        return entity;
    }

    @ApiOperation(value = "获取企业员工配件列表",notes = "http://120.55.12.229:9001/eam/swagger-ui.html#!/EquPartInfoController/getEnterpriseStaffEquPartPageUsingPOST")
    @ApiImplicitParam(name = "reqSearchEquPart",value = "reqSearchEquPart参数",dataType = "ReqSearchEquPart")
    @RequestMapping(value = "/equ/accessory",method = RequestMethod.POST)
    public Object getEqu(HttpServletRequest request, @RequestBody ReqSearchEquPart reqSearchEquPart){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        String staffId = UserDataUtil.getStaffId(request);
        String postUrl = eamApiUrl+"/eam/v1.0/"+enterpriseId+"/"+staffId+"/equ_parts/search";
//        ResponseEntity entity = restTemplate.getForEntity(postUrl,Object.class);
        Object entity  = restTemplate.postForObject(postUrl,reqSearchEquPart,Object.class);
        return entity;
    }

//    equipments/common_attrs
    @ApiOperation(value = "获取企业员工配件列表",notes = "http://120.55.12.229:9001/eam/swagger-ui.html#!/EquInfoController/getEquipmentCommonAttrsUsingGET")
    @RequestMapping(value = "/equ/common_attrs",method = RequestMethod.GET)
    public Object commonAttr(@ApiParam(value = "industry_type",name="industry_type",required = true) @RequestParam("industry_type") String industryType,
                             @ApiParam(value = "industry_id",name="industry_id",required = false) @RequestParam(value = "industry_id",required = false) String industryId,
                             HttpServletRequest request){
        String getUrl = eamApiUrl+"/eam/v1.0/equipments/common_attrs?industry_type="+industryType;

        if(!StringUtils.isEmpty(industryId)){
            getUrl+="&industry_id="+industryId;
        }
//        Object object = restTemplate.getForObject(getUrl,Object.class);

        HttpHeaders headers = new HttpHeaders();
        headers.add("session_id", request.getHeader("session_id"));
        HttpEntity<Object> entity = new HttpEntity<>(null, headers);
        ResponseEntity<Object> exchange =
                restTemplate.exchange(getUrl, HttpMethod.GET, entity, Object.class);
        return exchange.getBody();


    }

}
